<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <title><spring:message code="message.listForm.DingDanQueRen"/></title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp"%>
</head>

<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">

    <section class="content-header">
        <h1>订单审核</h1>
        <ol class="breadcrumb">
        	<li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
        	<li><a href="#"><spring:message code="message.list.DingDanGuanLi"/></a></li>
            <li><a href="#">订单审核</a></li>
        </ol>
    </section>
    
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
                <div class="input" style="margin-top:-5px;">
                    <div class="textalign1">配送企业<spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="delCompName" name="delCompName" placeholder="配送企业/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
                <div class="textalign1"><spring:message code="message.listForm.DingDanBaoHao"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="orderPackId" name="orderPackId" placeholder="<spring:message code="message.listForm.DingDanBaoHao"/>" />
                    </div>
                    <div class="textalign1"><spring:message code="message.listForm.CaiGouRiQi"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput"> 
                       <table style="width:100%">
                                <tr>
                                    <td style="width:50%">
                                        <input id="startDate" name="startDate" type="text" class="textinput" placeholder="yyyy-MM-dd" 
                                            data-date-fmt="yyyy-MM-dd" onFocus="WdatePicker({maxDate:'#F{$dp.$D(\'endDate\')}'})" />
                                    </td>
                                    <td>&nbsp;-&nbsp;</td>
                                    <td style="width:50%">
                                        <input id="endDate" name="endDate" type="text" class="textinput"  placeholder="yyyy-MM-dd" 
                                        data-date-fmt="yyyy-MM-dd" onFocus="WdatePicker({minDate:'#F{$dp.$D(\'startDate\')}'})" /> 
                                    </td> 
                                </tr>
                            </table>
                    </div>
                    <div class="textalign1" >
                    <button type="button" class="btn btn-success btn-sm" onclick="search()" id="selectDicCompType"><spring:message code="message.button.seachSpacing"/></button>
                    </div> 
                  </div> 
            </div>
        </div>
        
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        <div class=" box box-warning bottongroup">
             <label class="btn btn-primary btn-sm" onclick="auditHospOderInfo(11)">审核通过</label>
             <label class="btn btn-primary btn-sm" onclick="auditHospOderInfo(12)">审核不通过</label>
        </div>
        
    </section>
    
    <%@ include file="/WEB-INF/component/commonJS.jsp"%>
    <script type="text/javascript">
        function showOrderDetail(orderPackId) {
            $.HN.window.open({
                "id" : "orderDetail",
                "title" : "订单明细",
                "url" : "${ctx}/purOrderInfo/toOrderDetailByAudit.html?orderPackId=" + orderPackId
            });
        }
        
        function orderDetailLink(orderPackId, text) {
            return "<a href='javascript:showOrderDetail(\"" + orderPackId + "\")' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -100px 0px' class='opIcon' title='" + text + "'></a>";
        }
    
        $(function () {
            //绑定回车
            $('#orderPackId,#startDate,#endDate,#delCompName').bind('keydown', function(event) {
                if (event.keyCode == "13") {
                    $('#selectDicCompType').click();
                }
            }); 
            
            $("#delCompName").focus();//加载页面光标在第一个查询框内
            
            $("#gridlist").jqGrid({
                url: "getNotAuditHospOderList.html",
                mtype: "POST",
                datatype: "json",
               //  shrinkToFit: false,
                autowidth: true,
                height: 300,
                colNames: [
                    '操作',
                    '<spring:message code="message.listForm.DingDanBaoHao"/>', 
                    '配送企业',
                    '<spring:message code="message.listForm.CaiGouShiJian"/>',
                    '<spring:message code="message.listForm.TiaoMuShu"/>', 
                    '<spring:message code="message.listForm.ZongJinE"/>',
                    '<spring:message code="message.listForm.JiaJiDingDan"/>',
                    '收货人',
                    '收货人电话',
                    '送货地址',
                    '<spring:message code="message.lable.hospRemark"/>'
                ],
                colModel: [
                    { name: 'operate', width: 60 , align:"center" ,sortable: false ,
                        formatter:function(cellvalue, options, row){
                            return orderDetailLink(row.orderPackId, '明细');
                        } 
                    },
                    { name: 'orderPackId', width: 250 , align:"center" ,sortable: false },
                    { name: 'delCompName', width: 250 , align:"center" ,sortable: false/* ,
                    	formatter:function(cellvalue, options, row){
                            return compInfoLink(row.delCompCode,row.delCompName, 1);
                        } */
                    },
                    { name: 'submitDatetime', width: 200 , align:"center" ,sortable: false},
                    { name: 'count', width: 80 , align:"center",sortable: false},
                    { name: 'amount', width: 80 , align:"right",sortable: false,
                        formatter : function(val, opts, row) {
                            return formatAmount(val);
                        }
                    },
                    { name: 'isUrgent', width: 80 , align:"center",sortable: false,
                        formatter : function(val, opts, row) {
                            if (val == 1) {
                                return "<span class='red'>是</span>";
                            }
                            return "否";
                        }
                    },
                    { name: 'recpUser', width: 100 , align:"center" ,sortable: false },
                    { name: 'recpUserphone', width: 120 , align:"center" ,sortable: false },
                    { name: 'recpAddr', width: 250 , align:"left" ,sortable: false },
                    { name: 'remarks', width: 250 , align:"left" ,sortable: false }
                    
                ],
                rowNum: 20,
                rowList: [10, 20, 50, 100],
                rownumbers: true,
                multiselect: true,
                pager: "#gridpage",
                viewrecords: true,
                gridComplete:function(){
                    //序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                    //数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
                },
                caption: "未完成订单列表",
                jsonReader: {
                    repeatitems: false,
                    id: "orderPackId"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $("#gridlist").jqGrid('setGridHeight', create());
            $(window).trigger("resize");
            
            
        });
        
        
        /* 查询 */
        function search(page) {
            var orderPackId = $.trim($("#orderPackId").val());
            var delCompName = $.trim($("#delCompName").val());
            var startDate = $("#startDate").val();
            var endDate = $("#endDate").val();
            $("#gridlist").jqGrid("clearGridData");
                $("#gridlist").jqGrid('setGridParam',{  
                mtype: "POST",
                datatype:'json',
                postData:{
                    "orderPackId":orderPackId,
                    "startDate":startDate,
                    "endDate":endDate,
                    "delCompName":delCompName
                },
                page : (page || 1)
            }).trigger("reloadGrid"); //重新载入  
        }
        
        
        //审核
        function auditHospOderInfo(flag){
			var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
    		var statusName = "审核不通过？";
    		var url ="${ctx}/purOrderInfo/notPass.html";
        	if (flag == 11){
        		statusName = "审核通过？";
        		url ="${ctx}/purOrderInfo/pass.html";
        	}
			/* for (var i=0; i<rowIds.length; i++) {
				rowId = rowIds[i];
				var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
			} */
			var delCompName="";
        	for (var i=0; i<rowIds.length; i++) {
            	 var rowId = rowIds[i];
            	 delCompName += rowId + (i < rowIds.length - 1 ? "," : "");
                 var rowData = $("#gridlist").jqGrid("getRowData", rowId);
                 if(i<3){
                	   statusName += "<br/>" + rowData.delCompName + "-" + rowId ;    
                	   if(i==2){
                		   statusName += "<br/>...";
                	   }
                 }
             }
        	//获取选中的ID及对应的名字（参数:jqgridId,名字列名，状态列名，当前操作即列表中显示的值），返回infos[0]是ID串，infos[1]是名字串
        	 var infos = batchSwitchStatus("gridlist","orderPackId","isUrgent",statusName,"订单包号！");
        	if ( !(infos instanceof Array)){
        		return;
        	} 
        	
        	
        	var attionInfo  = ''; 
           	var isHisRecordAmount = ${isHisRecordAmount};
        	if(isHisRecordAmount && flag == 11){
        		$.post("${ctx}/purOrderInfo/checkHisRecordAmount.html",{param:infos[0]},function(result){
        			if(result.success){
        				var data = {
		    					"nonEsAmount":0,//页面勾选的非备案药物金额
		    					"esAmount":0,//页面勾选的备案药物金额
		    					"baseAllAmount":0,//备案药物金额
		    					"nonBaseAllAmount":0,//非备案药物金额
		    					"allAmount":0//药物采购总金额
		    			}
		    			$.each(result.rows,function(i,value){
		    				if(value.goodsSource==5){
		    					data.esAmount+=Number(value.proPrice)*Number(value.proCount);
		    				}else{
		    					data.nonEsAmount+=Number(value.proPrice)*Number(value.proCount);
		    				}
		    			});
        				data.nonBaseAllAmount =(data.nonEsAmount + Number(result.form.non_base_amount));
		    			data.baseAllAmount = (data.esAmount + Number(result.form.base_amount));
		    			data.allAmount = (data.nonEsAmount + data.esAmount + Number(result.form.non_base_amount) + Number(result.form.base_amount));
		    			var res = ((data.baseAllAmount*10000)/(data.allAmount*10000));
		    			console.log(data);
	    				console.log(res);
		    			if(res>0.02){
		    				confirm("以下订单包中备案采购金额占比超过<span class = 'red' >2%</span>,是否继续"+statusName,data);
		    			}else{
		    				confirm("以下订单包号"+statusName+"<br/>",data);
		    			}
        			}else{
        			    top.$.HN.message.alert("<spring:message code='message.HN.alert.fail' />","", "error");
        			    return ;
        			}
        		},"json")
        	}else{
        		confirm("以下订单包号"+statusName+"<br/>",null);
        	}
     
        	
        	function confirm(attionInfo,data){
        		var postData = {};
        		if(data!=null){
        			postData = data;
        		}
        		postData["state"] = flag;
    			postData["param"] = infos[0];
    			
        	     $.HN.message.confirm(attionInfo, '', '').on(function (e) {
                     if (e) {
                     	$.ajax({
                             url: url,//action地址
                             data: postData,//传值
                             type: "post",//执行方式get or post 默认以post方式执行
                             dataType:"json",
                             success: function (result) {//执行成功后 提示
                                 if (result.success){
                                 	top.$.HN.message.alert("<spring:message code='message.HN.alert.chengGongCaoZuo' />"+infos[0].split(",").length+"<spring:message code='message.HN.alert.tiaoShuJu' />","", "success");
                                     search( $("#gridlist").getGridParam("page"));
                                 } else {
                             	   top.$.HN.message.alert("<spring:message code='message.HN.alert.fail' />","", "error");
                                 } 
                             }
                         });
                     }
                 });
        	}
        	
        	
        	
        }
        
        
        
        
        
    </script>
</body>
</html>